package aam230713

import (
	"fmt"
	"strconv"
	"strings"
)

var menuItems = []struct {
	title string
	f     func(string) error
}{
	{"Add Player", addPlayer},
	{"Show Team", showPlayers},
	{"Edit Player", editPlayer},
	{"Delete Player", deletePlayer},
}

func getStudentRoute(number string) *string {
	sts := GetAny(DoRequest("GET", "", "", nil)).(map[string]any)["sts"]
	for _, st := range sts.([]any) {
		id := int(st.([]any)[0].(float64))
		title := st.([]any)[1].(string)
		if strings.Contains(title, number) {
			st := "st" + strconv.Itoa(id) + "/"
			return &st
		}
	}
	return nil
}

func Menu() bool {
	studentRoute := getStudentRoute("[2304-19]")
	if studentRoute == nil {
		return false
	}
	for {
		fmt.Println("--------------")
		fmt.Println("Choose action. 0 - to exit")
		for i, m := range menuItems {
			fmt.Println(i+1, m.title)
		}

		var i int
		if _, e := fmt.Scanf("%d\n", &i); e != nil || i < 1 || i > len(menuItems) {
			fmt.Println("exit")
			return false
		}
		err := menuItems[i-1].f(*studentRoute)
		if err != nil {
			fmt.Println(err)
			return false
		}
	}
}
